<?php

class ems{
	
	private static $cache_time = 7200;
	/**
	 * 取得 ems_msgs 数据列表
	 *
	 * @param int $catid 分类id
	 * @param int $num 返回数量
	 * @param int $title_len 标题长度
	 * @return array
	 */
	
	public static function get_message_list($catid,$num=4,$title_len=15){
		$catid = intval($catid);
		$num = intval($num);
		$title_len = intval($title_len);
		
		$Cache_Lite = MemoryCache::getInstance();
		$Cache_key = md5('get_message_list_'.$catid.'-'.$num.'-'.$title_len);
		if(!$return = $Cache_Lite->get($Cache_key)){
			
	        $db = DB_MySQLi::getInstance(1);
//	        $now_time = time();
	        $db->query("select id,title,its from ems_msgs 
	                     where status = 1 
	                     and cid = {$catid} 
	                   order by depth desc limit ".$num);
//	                     and onts <= {$now_time} 
//	                     and if(offts=0, 9999999999, offts) >= {$now_time}
	        $return = array();
	        while ($row = $db->fetchRow_ASSOCS()) {
	        	$row['title'] = self::_parse_str($row['title']);
	        	$row['short_title'] = Handler_string::utf8_substr($row['title'],$title_len,'..');
	            $return[] = $row;
	        }
	       $Cache_Lite->set($Cache_key,$return,self::$cache_time);			
		}
        return $return;
	}
	
	/**
	 * 取得友情连接
	 *
	 * @param int $catid 分类id
	 * @param int $num 返回条数，如果为0返回所有
	 * @return array
	 */
	public static function get_link_list($catid,$num=15){
		
		$catid = intval($catid);
		$num = intval($num);
		
		$Cache_Lite = MemoryCache::getInstance();
		$Cache_key = md5('get_link_list_'.$catid.'-'.$num);
		if(!$return = $Cache_Lite->get($Cache_key)){
            $limit = ($num > 0) ? " limit {$num} " : '' ;
	        $db = DB_MySQLi::getInstance(1);
	        $db->query("select id, title, url, img, local_img from ems_links 
	                     where status = 1 
	                     and cid = {$catid} 
	                   order by depth asc ".$limit);
	        $return = array();
	        while ($row = $db->fetchRow_ASSOCS()) {
	            $return[] = $row;
	        }
	       $Cache_Lite->set($Cache_key,$return,self::$cache_time);			
		}
        return $return;
	}

	
	/**
	 * 替换标题中的特殊字符
	 *
	 * @param str $str 标题
	 * @return str
	 */
	private function _parse_str($str){
		$special_char = array("{", "}", "[", "]", "$", "|", "#","[","]");
		$special_code = array("_LB_", "_RB_", "_lb_", "_rb_", "_D_", "_vl_", "_I_","【","】");
		$str = str_replace($special_code, $special_char, $str);
		return $str;
	}
	
}

?>